package com.example.yz.happywoker;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetCallback;
import com.avos.avoscloud.SaveCallback;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class DetailedInforMapActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView title;
    private TextView address;
    private TextView phone;
    private TextView content;
    private TextView date;

    private String status_Detail;
    private String id;
    //定时器：定时从后台读取数据
    private int i = 0;
    private int TIME = 1000;
    private AVUser currentUser;
    private  String EmployerID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailed_infor_map);
        title = (TextView) findViewById(R.id.title);
        address = (TextView) findViewById(R.id.address);
        phone = (TextView) findViewById(R.id.phone);
        content = (TextView) findViewById(R.id.content);
        date = (TextView) findViewById(R.id.date);
        Button button_Datail = (Button) findViewById(R.id.button_Datail);
        ImageView suggestion_back01 = (ImageView) findViewById(R.id.suggestion_backmap);
        suggestion_back01.setOnClickListener(this);
        Intent intent = getIntent();
        id = intent.getStringExtra("id");

        AVQuery<AVObject> avQuery = new AVQuery<>("MyOrderList");
        avQuery.getInBackground(id, new GetCallback<AVObject>() {
            @Override
            public void done(AVObject avObject, AVException e) {
                if (avObject.get("RepairCategory") != null) {
                    title.setText(avObject.get("RepairCategory").toString());
                }
                if (avObject.get("RealAddress") != null) {
                    address.setText(avObject.get("RealAddress").toString());
                }
                if (avObject.get("EmployerID") != null) {
                    phone.setText(avObject.get("EmployerID").toString());
                    EmployerID=avObject.get("EmployerID").toString();
                }
                if (avObject.get("ServiceContent") != null) {
                    content.setText(avObject.get("ServiceContent").toString());
                }
                if (ConverToString(avObject.getCreatedAt()) != null) {
                    date.setText(ConverToString(avObject.getCreatedAt()));
                }
                Log.e("qqq", "" + avObject.toString());
                Log.e("www", "" + avObject.get("RepairCategory"));
                if (avObject.get("Status") != null) {
                    status_Detail = avObject.get("Status").toString();
                }
                // object 就是 id 为 558e20cbe4b060308e3eb36c 的 Todo 对象实例
            }
        });
        button_Datail.setOnClickListener(new View.OnClickListener() {
            //申请订单
            @Override
            public void onClick(View v) {
                if (status_Detail.equals("1")) {
                    if (AVUser.getCurrentUser().get("Busy") == null || AVUser.getCurrentUser().get("Busy").equals("0"))//判断工人状态
                    {
//工人创建订单，将订单ID传输到RobOrderList中，同时改变订单状态，通过定时器不断查询状态是否改变（雇主是否更改状态），如状态更改则表示抢单成功。
                        final AVObject todoFolder = new AVObject("RobMyOrderList");// 构建对象
                        todoFolder.put("OrderNumber", id);//设置订单号
                        id=null;
                        todoFolder.put("RoberName", AVUser.getCurrentUser().get("Name").toString());// 设置用户姓名
                        todoFolder.put("State", "1");// 设置订单状态
                        todoFolder.put("RoberId", AVUser.getCurrentUser().getObjectId());
                        todoFolder.put("robWokerPhone", AVUser.getCurrentUser().getUsername());
                    //    todoFolder.put("robBirth", AVUser.getCurrentUser().get("DateBirth").toString());
                        todoFolder.put("EmployerID", EmployerID);
                        todoFolder.saveInBackground(new SaveCallback() {
                            @Override
                            public void done(AVException e) {
                                if (e == null) {
                                    // 存储成功
                                    GlobalData.IdG=null;// 保存成功之后，objectId 会自动从服务端加载到本地
                                    Toast.makeText(DetailedInforMapActivity.this,"提交成功，等待雇主确认",Toast.LENGTH_LONG).show();
                                    finish();
                                } else {
                                    // 失败的话，请检查网络环境以及 SDK 配置是否正确
                                }
                            }
                        });

                    } else {
                        Toast.makeText(getApplication(), "有尚未完成订单 暂时无法申请", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(getApplication(), "无法申请", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //把日期转为字符串
    public static String ConverToString(Date date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(date);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.suggestion_backmap:
                finish();
                break;
        }
    }
}
